<template>
  <div id="userUpdate" style="padding: 20px;">
    <el-form ref="form" :model="sysUser" :rules="rules" label-width="80px" size="small">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账号" prop="username">
            <el-input v-model="sysUser.username" readonly placeholder="请输入账号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="头像" prop="header">
            <ImageUpload :url="sysUser.header" @loadImageUrl="loadImageUrl" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="角色">
            <el-select v-model="sysUser.roleIds" style="width: 215px" multiple placeholder="请选择">
              <el-option
                v-for="item in roleOptions"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="昵称" prop="nickName">
            <el-input v-model="sysUser.nickName" placeholder="请输入昵称" clearable/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="sysUser.email" placeholder="请输入邮箱" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="sysUser.note" :rows="2" type="textarea" placeholder="请输入备注" clearable/>
          </el-form-item>
        </el-col>
      </el-row>

      <div class="dialog-footer">
        <el-button v-if="!loading" type="primary" @click="update">提 交</el-button>
        <el-button v-else type="primary">提交中...</el-button>
        <el-button type="warning" @click="sysUser = {}">重 置</el-button>
      </div>
    </el-form>

  </div>
</template>

<script>
import sysUserApi from '@/api/system/sys-user'
import roleApi from '@/api/system/sys-role'
import { hideFullScreenLoading, showFullScreenLoading } from '@/utils/loadIng'
import ImageUpload from '@/components/Upload/ImageUpload.vue'

export default {
  components: { ImageUpload },
  props: {
    // 调用方传来的用户ID
    activeId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      imageUrl: '',
      loading: false,
      // 表单数据
      sysUser: {
        // 反选角色集合
        roleIds: []
      },
      // 角色集合
      roleOptions: [],
      // 表单校验对象
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, message: '用户名最短5位', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, message: '密码最短5位', trigger: 'change' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        email: [
          {
            type: 'email',
            message: '\'请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  watch: {
    activeId: {
      immediate: true,
      handler: function(newVal, oldVal) {
        this.getById(newVal)
      }
    }
  },
  created() {
    showFullScreenLoading('#userUpdate')
    this.loadRoleList()
    this.getById()
  },
  methods: {
    // 根据id查询用户
    getById() {
      if (this.activeId !== '' || this.activeId != null) {
        sysUserApi.getById(this.activeId).then(res => {
          this.sysUser = res.data
          const roles = []
          this.sysUser.sysRoleList.map(e => {
            if (e !== null && e.roleId !== null) {
              roles.push(e.roleId)
            }
          })
          this.sysUser.roleIds = roles
        })
        hideFullScreenLoading()
      }
    },
    // 修改用户
    update() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          sysUserApi.update(this.sysUser).then(res => {
            this.$message.success(res.msg)
            // 关闭对话框，并刷新列表
            this.$emit('close')
            this.$emit('after')
            this.loading = false
          })
        }
      })
    },
    loadRoleList() {
      roleApi.getRoleAll().then(res => {
        this.roleOptions = res.data
      })
    },
    // 回显头像
    loadImageUrl(path) {
      this.sysUser.header = path
    }
  }
}
</script>

<style>

</style>
